import os
import numpy as np
from matplotlib import pyplot as plt
from PIL import Image
from random import shuffle
os.chdir('SVM')
WIDTH = 403
HEIGHT = 755

global data,label,group
data = np.empty((0,HEIGHT,WIDTH),dtype=np.uint8)
label = np.empty((0),dtype=np.uint8)
group = np.empty((0),dtype=np.uint8)

def dataloader(item,filepath):
    p = filepath.split('\\')
    error_type = int(p[3][0])
    group_type = 1 if p[2]=='test' else 0 #0 test, 1 train

    path = os.path.join(filepath,item)
    img = Image.open(path).convert('L').resize((WIDTH,HEIGHT))
    img = np.array(img)
    global data,label,group
    data = np.concatenate((data,img[None]))
    label = np.concatenate((label,np.array([error_type])))
    group = np.concatenate((group,np.array([group_type])))
    pass



filepath_list=['dataset\\raw']
filelist=[]
for filepath in filepath_list:
    files = os.listdir(filepath)
    i = 1
    for fi in files:
        fi_d = os.path.join(filepath, fi)
        if not (os.path.isdir(fi_d)):
            filelist.append(fi)
            dataloader(fi,filepath)
            print('\r' + str(i) + '/' + str(len(files)),end='    ')
            i+=1
        else:
            filepath_list.append(fi_d)
pass

index = [i for i in range(len(data))]
shuffle(index)

np.save('dataset\\standard\\data.npy',data[index])
np.save('dataset\\standard\\label.npy',label[index])
np.save('dataset\\standard\\group.npy',group[index])